package com.hylanda.va.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.hylanda.va.entity.SysOrders;
import com.hylanda.va.model.OrderAgg;
import com.hylanda.va.model.OrderAggOrderTotal;
import com.hylanda.va.model.OrderQuery;
import com.hylanda.va.service.OrderService;

/** 
 * @author zhangy
 * @E-mail:blackoon88@gmail.com 
 * @qq:846579287
 * @version created at：2018年5月15日 下午3:43:55 
 * note
 */
@RestController
@RequestMapping("/order")
public class OrderRestController {
	
	@Autowired
	public OrderService orderService;
	
	//生成订单
	@RequestMapping(value = { "/save" }, method = RequestMethod.POST)
	public JSONObject insert(HttpServletRequest request,
			HttpServletResponse response,@RequestBody @Valid SysOrders model){
		SysOrders o=orderService.save(model);
		JSONObject jo=new JSONObject();
		jo.put("result", "success");
		jo.put("data", o);
		return jo;
	}
	//查询
	@RequestMapping(value={"/search"},method ={RequestMethod.GET,RequestMethod.POST})
	public JSONObject getOrders(HttpServletRequest request,
			HttpServletResponse response,@RequestBody OrderQuery orderQuery){
		Page<SysOrders> page=orderService.findOrderCriteria(orderQuery.getPage(), orderQuery.getSize(), orderQuery);
		JSONObject jo=new JSONObject();
		jo.put("data", page);
		return jo;
	}
	//统计分析工作室营收情况
	@RequestMapping(value={"/agg/studio"},method={RequestMethod.GET,RequestMethod.POST})
	public JSONObject getAggStudio(HttpServletRequest request,
			HttpServletResponse response,@RequestBody OrderQuery orderQuery){
		List<OrderAgg> aggs=orderService.getAggStudio(orderQuery);
		JSONObject jo=new JSONObject();
		jo.put("data", aggs);
		return jo;
	}
	//统计工作室订单量
	@RequestMapping(value={"/agg/studio/ordertotal"},method={RequestMethod.GET,RequestMethod.POST})
	public JSONObject getAggStudioOrderTotal(HttpServletRequest request,
			HttpServletResponse response,@RequestBody OrderQuery orderQuery){
		List<OrderAggOrderTotal> aggs=orderService.getAggStudioOrderTotal(orderQuery);
		JSONObject jo=new JSONObject();
		jo.put("data", aggs);
		return jo;
	}
}
